package com.techstar.ats.codec.history.decoder;

import io.netty.buffer.ByteBuf;
import com.techstar.ats.codec.Decoder;
import com.techstar.ats.dxp.CsvFormatter;
import com.techstar.ats.dxp.history.rep.AbstractReportItem;

public interface ReportItemDecoder<E extends CsvFormatter, T extends AbstractReportItem<E>> extends ReportDecoder<T> {
	Decoder<E> itemDecoder();

	default void decode(ByteBuf buf, T value) {
		ReportDecoder.super.decode(buf, value);
		value.total = buf.readUnsignedShort();
		value.sequence = buf.readUnsignedShort();
		int cnt = buf.readUnsignedShort();

		Decoder<E> itemDecoder = itemDecoder();
		for(int i = 0; i < cnt; ++i) {
			E item = itemDecoder.create();
			itemDecoder.decode(buf, item);
			value.items.add(item);
		}
	}
}
